package com.hyyt.shopjsp.customer.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.customer.pojo.ShopEvaluateGoods;
import com.hyyt.shopjsp.customer.service.IShopEvaluateGoodsService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/31 0024下午 16:53.
 */
@Controller
@RequestMapping("/back/evalGoods")
public class ShopEvaluateGoodsController extends BaseController {
	private Logger log = Logger.getLogger(ShopEvaluateGoodsController.class);
	private String basePath = "/back/shop/customerEvaluateGoods/";

	@Resource
	private IShopEvaluateGoodsService shopEvaluateGoodsService;//会员评价service
	/**
	 * 进入会员评价管理
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoEvaluateGoods")
	public String gotoEvaluateGoods(){
		return basePath+"evaluateGoodsList";
	}

	/**
	 * 查询所有会员评价信息
	 * @param ordersNo 订单编号
	 * @param appraiserName 评价人
	 * @param shopName 店铺名称
	 * @param level 评价等级
	 * @param evaluateState 评价状态
	 * @param productId 商品Id
	 * @param page 分页
	 * @param response 返回结果集
	 */
	@RequestMapping(value="listEvalGoods",method=RequestMethod.POST)
	@ResponseBody
	public void listEvalGoods(String ordersNo, String appraiserName, String shopName, String level, String evaluateState,Integer productId, Page page, HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if(Utils.objectIsNotEmpty(ordersNo)){//订单编号
			param.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(shopName)){//店铺名称
			param.put("shopName", shopName.trim());
		}
		if(Utils.objectIsNotEmpty(appraiserName)){//评价人
			param.put("appraiserName", appraiserName.trim());
		}
		if(Utils.objectIsNotEmpty(level)){//评价等级
			param.put("level", level);
		}
		if(Utils.objectIsNotEmpty(evaluateState)){//评价状态
			param.put("evaluateState", evaluateState);
		}
		if(Utils.objectIsNotEmpty(productId)){
			param.put("productId", productId);
		}
		param.put("sortColumns", "evaluateId desc");
		int totalRecordCount =shopEvaluateGoodsService.countShopEvaluateGoods(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopEvaluateGoods> shopEvaluateGoodsList =shopEvaluateGoodsService.queryShopEvaluateGoods(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", shopEvaluateGoodsList);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据会员评价id获得对象
     *
     * @param evaluateId 会员评价Id
     */
    @RequestMapping(value = "getEvaluateGoods", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getEvaluateGoods(Integer evaluateId) {
		if (Utils.objectIsNotEmpty(evaluateId)) {
			ShopEvaluateGoods evaluateGoods = shopEvaluateGoodsService.loadShopEvaluateGoods(evaluateId);
			return ResultMsg.create(true, null, FastJsonUtils.toJSONString(evaluateGoods));
		}else {
			return ResultMsg.create(false);
		}
	}

    /**
     * 修改会员评价
     *
	 * @param shopEvaluateGoods 会员评价对象
	 * @return 返回布尔值
     */
    @RequestMapping(value = "updateContentAndEvaluateState", method = RequestMethod.POST)
    @ResponseBody
	public ResultMsg updateContentAndEvaluateState(ShopEvaluateGoods shopEvaluateGoods) {
		if (Utils.objectIsNotEmpty(shopEvaluateGoods)){
			if (Utils.objectIsNotEmpty(shopEvaluateGoods.getEvaluateId())){
				shopEvaluateGoodsService.updateShopEvaluateGoods(shopEvaluateGoods);
			}
			return ResultMsg.create(true);
		}else {
			return ResultMsg.create(false);
		}
	}

}